
<?php
/**************************用户登陆异常****************************************/
defined('BASEPATH') or die('forbidden');

/*
 * 发送的数据大概如下
{
	"event":[
	{"networkType":"WIFI","deviceSystem":"Android4.1","deviceName":"M040","eventId":"1000","deviceid":"2342523"},
	{"networkType":"WIFI","deviceSystem":"Android4.1","deviceName":"M040","eventId":"1000","deviceid":"2342523"},
	{"networkType":"WIFI","deviceSystem":"Android4.1","deviceName":"M040","eventId":"1000","deviceid":"2342523"},
	{"networkType":"WIFI","deviceSystem":"Android4.1","deviceName":"M040","eventId":"1000","deviceid":"2342523"},
	{"networkType":"WIFI","deviceSystem":"Android4.1","deviceName":"M040","eventId":"1000","deviceid":"2342523"}
	]
} 
 */

class ReceiveReport_Controller extends  Controller {
	
	private $recdb;	//用户登陆日志
	
    function _init() {
    	require_once(CONFIGPATH . 'db.php');
    	require_once(CONFIGPATH . 'inc.php');
    	//print_r($config['buglist']);
    	
    	$this->recdb = new Database($config['buglist']);
    	//$this->load->database($config['buglist']);
    	//$this->buglist->select_db("crashreport");
    	//$this->buglist->select_db("buglist");	//for test

    	$sql = "SELECT *  FROM eventtype ";
        $this->recdb->query($sql);
        $eventTypeList = array();	//事件列表
    	while ($n = $this->recdb->get_array('default')) 
    	{
   			$eventTypeList[$n['eventID']] = $n['eventValue'];
        } 
    	//print_r($eventTypeList);
    	$receiveData = file_get_contents('php://input');
    	
    	//for test
    	/*
		$receiveData = '{
			"event":[
			{"networkType":"WIFI","deviceSystem":"Android4.1","deviceName":"M040","eventId":"1000","deviceid":"2342523"},
			{"networkType":"WIFI","deviceSystem":"Android4.1","deviceName":"M040","eventId":"1000","deviceid":"2342523"},
			{"networkType":"WIFI","deviceSystem":"Android4.1","deviceName":"M040","eventId":"1000","deviceid":"2342523"},
			{"networkType":"WIFI","deviceSystem":"Android4.1","deviceName":"M040","eventId":"1000","deviceid":"2342523"},
			{"networkType":"WIFI","deviceSystem":"Android4.1","deviceName":"M040","eventId":"1000","deviceid":"2342523"}
			]
		}';
		*/	
    	/*
    	$receiveData = '{"event":[{"retryTimes":1,"accountID":0,"platform":1,"trackID":"e69db7b1-d696-469b-aed9-ad5a8c7e3eaa",
    	"osVer":"4.3","gameID":1,"deviceID":"d3d457f1c68d4bbc","zoneID":0,"SDKVer":"","startTime":"2014-07-04 13:37:29",
    	"netWorkType":0,"deviceName":"L36h","eventId":1000,"gameVer":"0.6.0","channel":"gfan","channelUserId":""}]}';
    	*/
    	//$receiveData = '{"event":[{"accountID":0,"platform":1,"trackID":"e69db7b1-d696-469b-aed9-ad5a8c7e3e0f","osVer":"4.3","gameID":1,"deviceID":"d3d457f1c68d4bbc","zoneID":0,"SDKVer":"","startTime":"2014-07-04 13:37:29","netWorkType":0,"deviceName":"L36h","eventId":1000,"gameVer":"0.6.0","channel":"gfan","channelUserId":""},{"accountID":0,"platform":1,"trackID":"e69db7b1-d696-469b-aed9-ad5a8c7e3e0f","osVer":"4.3","gameID":1,"deviceID":"d3d457f1c68d4bbc","zoneID":0,"SDKVer":"","startTime":"2014-07-04 13:37:29","netWorkType":0,"deviceName":"L36h","eventId":1001,"gameVer":"0.6.0","channel":"gfan","channelUserId":""},{"accountID":0,"platform":1,"trackID":"e69db7b1-d696-469b-aed9-ad5a8c7e3e0f","osVer":"4.3","gameID":1,"deviceID":"d3d457f1c68d4bbc","zoneID":0,"SDKVer":"","startTime":"2014-07-04 13:37:44","netWorkType":0,"deviceName":"L36h","eventId":1002,"gameVer":"0.6.0","channel":"gfan","channelUserId":""},{"accountID":0,"platform":1,"trackID":"e69db7b1-d696-469b-aed9-ad5a8c7e3e0f","osVer":"4.3","gameID":1,"deviceID":"d3d457f1c68d4bbc","zoneID":0,"SDKVer":"","startTime":"2014-07-04 13:37:45","netWorkType":0,"deviceName":"L36h","eventId":1003,"gameVer":"0.6.0","channel":"gfan","channelUserId":""},{"accountID":0,"platform":1,"trackID":"e69db7b1-d696-469b-aed9-ad5a8c7e3e0f","osVer":"4.3","gameID":1,"deviceID":"d3d457f1c68d4bbc","zoneID":0,"SDKVer":"","startTime":"2014-07-04 13:37:45","netWorkType":0,"deviceName":"L36h","eventId":1004,"gameVer":"0.6.0","channel":"gfan","channelUserId":"27046152"},{"accountID":0,"platform":1,"trackID":"e69db7b1-d696-469b-aed9-ad5a8c7e3e0f","osVer":"4.3","gameID":1,"deviceID":"d3d457f1c68d4bbc","zoneID":0,"SDKVer":"","startTime":"2014-07-04 13:37:45","netWorkType":0,"deviceName":"L36h","eventId":1005,"gameVer":"0.6.0","channel":"gfan","channelUserId":"27046152"},{"accountID":0,"platform":1,"trackID":"e69db7b1-d696-469b-aed9-ad5a8c7e3e0f","osVer":"4.3","gameID":1,"deviceID":"d3d457f1c68d4bbc","zoneID":0,"SDKVer":"","startTime":"2014-07-04 13:37:45","netWorkType":0,"deviceName":"L36h","eventId":1006,"gameVer":"0.6.0","channel":"gfan","channelUserId":"27046152"},{"accountID":15586,"platform":1,"trackID":"e69db7b1-d696-469b-aed9-ad5a8c7e3e0f","osVer":"4.3","gameID":1,"deviceID":"d3d457f1c68d4bbc","zoneID":0,"SDKVer":"","startTime":"2014-07-04 13:37:46","netWorkType":0,"deviceName":"L36h","eventId":1007,"gameVer":"0.6.0","channel":"gfan","channelUserId":"27046152"},{"accountID":15586,"platform":1,"trackID":"e69db7b1-d696-469b-aed9-ad5a8c7e3e0f","osVer":"4.3","gameID":1,"deviceID":"d3d457f1c68d4bbc","zoneID":0,"SDKVer":"","startTime":"2014-07-04 13:38:51","netWorkType":0,"deviceName":"L36h","eventId":1008,"gameVer":"0.6.0","channel":"gfan","channelUserId":"27046152"},{"accountID":15586,"platform":1,"trackID":"e69db7b1-d696-469b-aed9-ad5a8c7e3e0f","osVer":"4.3","gameID":1,"deviceID":"d3d457f1c68d4bbc","zoneID":2,"SDKVer":"","startTime":"2014-07-04 13:38:51","netWorkType":0,"deviceName":"L36h","eventId":1009,"gameVer":"0.6.0","channel":"gfan","channelUserId":"27046152"},{"accountID":15586,"platform":1,"trackID":"e69db7b1-d696-469b-aed9-ad5a8c7e3e0f","osVer":"4.3","gameID":1,"deviceID":"d3d457f1c68d4bbc","zoneID":2,"SDKVer":"","startTime":"2014-07-04 13:39:08","netWorkType":0,"deviceName":"L36h","eventId":1011,"gameVer":"0.6.0","channel":"gfan","channelUserId":"27046152"}]}';	    	
		$jsonData = json_decode($receiveData, true);

		//echo "receive data";
    	//var_dump($jsonData);
    	//print_r($jsonData);
    	
    	$insertValue = array();
		foreach ($jsonData['event'] as $key=>$value)
		{
			foreach ($value as $subKey=>$subValue)
			{
				//echo "subkey:$subKey subvalue = $subValue";
				$insertValue[$key][$subKey] = $subValue;
			}
			//echo " next line ";
		}
		//print_r($insertValue);
		//fwrite("", $string)
    	//多条数据的插入
    	$errorNumber = 0;
		foreach ($insertValue as $key => $value)
		{		
			//先查下是否有这条信息，有的话，就把次数加1
			//根据数据流id与事件id来判断 是否已经有此记录
			//print_r($value);
			/*
			$sql = "select * from loginreport where trackId = '$value[trackID]' and eventId = $value[eventId] ";
			//echo " sql = $sql ";
			unset($row);
			$row = $this->recdb->execute($sql);
			//echo " row = $row ";
			if (false == $row) {
				++$errorNumber;
				//echo " false == row sql:$sql ";
				break;
			}
			*/
			if ($n = $this->recdb->get_array('default')) 
			{
				//更新次数
				$sql = "update loginreport set retrytimes = retrytimes + $value[retryTimes] where trackId = '$value[trackID]' and eventId = $value[eventId] ";
				
				if(false == $this->recdb->execute($sql))
				{
					++$errorNumber;
					//echo " excute error sql:$sql ";
					break;
				}else 
				{
					//继续处理下一条
					continue;
				}
			}
			
			//$value['retrytimes'] = 0;	//加上一个重复次数 字段
			$value['IP'] = $_SERVER["REMOTE_ADDR"];	//远程ip
			//echo "eventID = ".$value['eventId'];
			$value['eventValue'] = $eventTypeList[$value['eventId']];
			//echo "eventvalue = ".$value['eventValue'];
			$value['insertTime'] = date('Y-m-d H:i:s', time(NULL));;	//当前插入时间
			//print_r($value);
	    	if( false == $this->recdb->insert("loginreport", $value))
	    	{
	    		//echo "error";
	    		//echo " insert error sql:$sql ";
	    		++$errorNumber;
	    		break;
	    	}
		}
		if ( 0 ==$errorNumber && 0 < count($insertValue)) {
			echo "success";
		}else 
		{
			echo "fail";
		}
    }
    
    function index()
    {
    	//echo "index";
    }
}
?>